import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controllers/profile_controller.dart';
import '../../../utils/avatar_helper.dart';
import '../../../translations/app_translations.dart';

class ProfileView extends GetView<ProfileController> {
  const ProfileView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(LocaleKeys.profileTitle.tr),
        actions: [
          Obx(() => controller.isEditing.value
            ? Row(
                children: [
                  // 取消按钮
                  TextButton(
                    onPressed: controller.cancelEditing,
                    child: Text(LocaleKeys.cancel.tr, style: const TextStyle(color: Colors.white)),
                  ),
                  // 保存按钮
                  TextButton(
                    onPressed: controller.saveProfile,
                    child: Text(LocaleKeys.save.tr, style: const TextStyle(color: Colors.white)),
                  ),
                ],
              )
            : IconButton(
                icon: const Icon(Icons.edit),
                onPressed: controller.startEditing,
                tooltip: LocaleKeys.edit.tr,
              )
          ),
        ],
      ),
      body: Obx(() => controller.isLoading.value
        ? const Center(child: CircularProgressIndicator())
        : _buildProfileContent()
      ),
    );
  }

  Widget _buildProfileContent() {
    return SingleChildScrollView(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 头像区域
          Center(
            child: Column(
              children: [
                AvatarHelper.createAvatar(
                  radius: 50,
                  avatarUrl: controller.userService.avatar,
                ),
                const SizedBox(height: 8),
                // 用户名显示
                Text(
                  controller.userService.userName,
                  style: const TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 4),
                // 显示角色
                Chip(
                  label: Text(
                    controller.userService.roles.isNotEmpty
                      ? controller.userService.roles.join(', ')
                      : LocaleKeys.noRole.tr,
                    style: const TextStyle(color: Colors.white),
                  ),
                  backgroundColor: Colors.blue,
                ),
              ],
            ),
          ),
          
          const SizedBox(height: 24),
          const Divider(),
          const SizedBox(height: 16),
          
          // 个人资料部分
          Text(
            LocaleKeys.profileInfo.tr,
            style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          
          // 昵称
          _buildProfileItem(
            icon: Icons.person,
            title: LocaleKeys.nickname.tr,
            value: controller.userService.nickName,
            isEditing: controller.isEditing.value,
            onChanged: (value) => controller.nickName.value = value,
            editingValue: controller.nickName.value,
          ),
          
          // 邮箱
          _buildProfileItem(
            icon: Icons.email,
            title: LocaleKeys.email.tr,
            value: controller.userService.currentUser?.email ?? '',
            isEditing: controller.isEditing.value,
            onChanged: (value) => controller.email.value = value,
            editingValue: controller.email.value,
          ),
          
          // 手机号
          _buildProfileItem(
            icon: Icons.phone,
            title: LocaleKeys.phone.tr,
            value: controller.userService.currentUser?.phonenumber ?? '',
            isEditing: controller.isEditing.value,
            onChanged: (value) => controller.phone.value = value,
            editingValue: controller.phone.value,
          ),
          
          const SizedBox(height: 16),
          const Divider(),
          const SizedBox(height: 16),
          
          // 系统相关部分
          Text(
            LocaleKeys.systemInfo.tr,
            style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 16),
          
          // 用户ID
          _buildInfoItem(
            icon: Icons.numbers,
            title: LocaleKeys.userId.tr,
            value: controller.userService.currentUser?.userId.toString() ?? '',
          ),
          
          // 部门ID
          _buildInfoItem(
            icon: Icons.business,
            title: LocaleKeys.deptId.tr,
            value: controller.userService.currentUser?.deptId.toString() ?? '',
          ),
          
          // 账号状态
          _buildInfoItem(
            icon: Icons.verified_user,
            title: LocaleKeys.accountStatus.tr,
            value: controller.userService.currentUser?.status == '0' ? LocaleKeys.statusNormal.tr : LocaleKeys.statusDisabled.tr,
          ),
        ],
      ),
    );
  }

  // 构建个人资料项（可编辑）
  Widget _buildProfileItem({
    required IconData icon,
    required String title,
    required String value,
    required bool isEditing,
    required Function(String) onChanged,
    required String editingValue,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: Row(
        children: [
          Icon(icon, color: Colors.blue),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 14,
                    color: Colors.grey,
                  ),
                ),
                isEditing
                    ? TextField(
                        controller: TextEditingController(text: editingValue),
                        onChanged: onChanged,
                        decoration: const InputDecoration(
                          border: UnderlineInputBorder(),
                        ),
                      )
                    : Text(
                        value,
                        style: const TextStyle(fontSize: 16),
                      ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 构建只读信息项
  Widget _buildInfoItem({
    required IconData icon,
    required String title,
    required String value,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: Row(
        children: [
          Icon(icon, color: Colors.blue),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: const TextStyle(
                    fontSize: 14,
                    color: Colors.grey,
                  ),
                ),
                Text(
                  value,
                  style: const TextStyle(fontSize: 16),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
} 